package com.tanhua.dubbo.server.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Page;
import cn.hutool.system.UserInfo;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.enums.CommentType;
import com.tanhua.dubbo.server.enums.StatusEnum;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service(version = "1.0.0")
public class MessageDetailApiImpl implements MessageDetailApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Publish queryPublishByPublishId(String id) {

        return mongoTemplate.findById(new ObjectId(id), Publish.class);

    }



    @Override
    public List<Comment> queryCommentByPublishId(String id) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(id)));

        return mongoTemplate.find(query, Comment.class);
    }

    @Override
    public List<Comment> queryCommentByUserId(Integer uid) {
        Query query = Query.query(Criteria.where("userId").is(uid));
        return mongoTemplate.find(query, Comment.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pagesize
     * @param sortOrder
     * @param uid
     * @param state
     * @return
     */
    @Override
    public PageInfo<Publish> queryPublishByUserId(Integer page, Integer pagesize, String sortOrder, Integer uid, String state) {
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pagesize);

        PageRequest pageRequest;
        if (StrUtil.equals(sortOrder, "ascending")) {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.asc("created")));
        } else {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc("created")));
        }
        List<Publish> publishList = new ArrayList<>();
        //todo
        if (state.equals("all")) {
            Query query = new Query(Criteria.where("userId").is(uid)).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);
        } else {
            Query query = Query.query(Criteria.where("state").is(state).and("userId").is(uid)).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);
        }
        pageInfo.setRecords(publishList);
        return pageInfo;
    }


    /**
     * 模糊查询 通过用户id和日期
     * @param page
     * @param pagesize
     * @param sortOrder
     * @param uid 用户id
     * @param state
     * @param sd 起始时间
     * @param ed 结束时间
     * @return
     */
    @Override
    public PageInfo<Publish> queryPublishByUserIdAndDate(Integer page, Integer pagesize, String sortOrder, Integer uid, String state, Long sd, Long ed) {
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pagesize);


        PageRequest pageRequest;
        if (StrUtil.equals(sortOrder, "ascending")) {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.asc("created")));
        } else {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc("created")));
        }

        List<Publish> publishList = new ArrayList<>();
        //Todo
        if (state.equals("all")) {
            Query query = new Query(Criteria.where("userId").is(uid).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed))).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);

        } else {
            Query query = Query.query(Criteria.where("state").is(state).and("userId").is(uid).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed))).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);
        }
        pageInfo.setRecords(publishList);
        return pageInfo;
    }

    /**
     * 模糊查询 通过日期
     * @param page
     * @param pagesize
     * @param sortOrder
     * @param state
     * @param sd 起始时间
     * @param ed 结束时间
     * @return
     */
    @Override
    public PageInfo<Publish> queryPublishByDate(Integer page, Integer pagesize, String sortOrder, String state, Long sd, Long ed) {
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pagesize);

        PageRequest pageRequest;
        if (StrUtil.equals(sortOrder, "ascending")) {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.asc("created")));
        } else {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc("created")));
        }

        List<Publish> publishList = new ArrayList<>();

        if (state.equals("all")) {
            Query query = Query.query(Criteria.where("seeType").is(1).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed))).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);

        } else {
            Query query = Query.query(Criteria.where("state").is(state).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed))).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);
        }
        pageInfo.setRecords(publishList);
        return pageInfo;
    }

    @Override
    public List<Comment> queryAllComment() {
        return mongoTemplate.findAll(Comment.class);
    }


    /**
     * 查询已通过状态数量

     * @return
     */
    @Override
    public List<Publish> queryPass(Integer uid,Long sd, Long ed) {
        if (uid == null && (ed < 0L && sd < 0L)) {
            //查所有 没有模糊查询
            Query query = Query.query(Criteria.where("state").is(StatusEnum.PASS.getValue().toString()));
            return mongoTemplate.find(query, Publish.class);
        } else if(uid != null && (ed < 0L && sd < 0L)) {
            //模糊查询  通过uid  没有日期
            Query query = Query.query(Criteria.where("state").is(StatusEnum.PASS.getValue().toString()).and("userId").is(uid));
            return mongoTemplate.find(query, Publish.class);
        }else if(uid != null && (ed > 0L && sd > 0L)) {
            //模糊查询 通过uid和日期
            Query query = Query.query(Criteria.where("state").is(StatusEnum.PASS.getValue().toString()).and("userId").is(uid).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed)));
            return mongoTemplate.find(query, Publish.class);
        }else {
            //模糊查询 通过日期  没有uid
            Query query = Query.query(Criteria.where("state").is(StatusEnum.PASS.getValue().toString()).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed)));
            return mongoTemplate.find(query, Publish.class);
        }
    }


    /**
     * 查询待审核状态数量
     *
     * @return
     */
    @Override
    public List<Publish> queryCheck(Integer uid,Long sd, Long ed) {
        if (uid == null && (ed < 0L && sd < 0L)) {
            //查所有 没有模糊查询
            Query query = Query.query(Criteria.where("state").is(StatusEnum.UNREVIEWED.getValue().toString()));
            return mongoTemplate.find(query, Publish.class);
        } else if(uid != null && (ed < 0L && sd < 0L)) {
            //模糊查询  通过uid  没有日期
            Query query = Query.query(Criteria.where("state").is(StatusEnum.UNREVIEWED.getValue().toString()).and("userId").is(uid));
            return mongoTemplate.find(query, Publish.class);
        }else if(uid != null && (ed > 0L && sd > 0L)) {
            //模糊查询 通过uid和日期
            Query query = Query.query(Criteria.where("state").is(StatusEnum.UNREVIEWED.getValue().toString()).and("userId").is(uid).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed)));
            return mongoTemplate.find(query, Publish.class);
        }else {
            //模糊查询 通过日期  没有uid
            Query query = Query.query(Criteria.where("state").is(StatusEnum.UNREVIEWED.getValue().toString()).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed)));
            return mongoTemplate.find(query, Publish.class);
        }

    }


    /**
     * 查询已拒绝状态数量
     *
     * @return
     */
    @Override
    public List<Publish> queryReject(Integer uid,Long sd, Long ed) {
        if (uid == null && (ed < 0L && sd < 0L)) {
            //查所有 没有模糊查询
            Query query = Query.query(Criteria.where("state").is(StatusEnum.REJECTB.getValue().toString()));
            return mongoTemplate.find(query, Publish.class);
        } else if(uid != null && (ed < 0L && sd < 0L)) {
            //模糊查询  通过uid  没有日期
            Query query = Query.query(Criteria.where("state").is(StatusEnum.REJECTB.getValue().toString()).and("userId").is(uid));
            return mongoTemplate.find(query, Publish.class);
        }else if(uid != null && (ed > 0L && sd > 0L)) {
            //模糊查询 通过uid和日期
            Query query = Query.query(Criteria.where("state").is(StatusEnum.REJECTB.getValue().toString()).and("userId").is(uid).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed)));
            return mongoTemplate.find(query, Publish.class);
        }else {
            //模糊查询 通过日期  没有uid
            Query query = Query.query(Criteria.where("state").is(StatusEnum.REJECTB.getValue().toString()).andOperator(
                    Criteria.where("created").gte(sd), Criteria.where("created").lte(ed)));
            return mongoTemplate.find(query, Publish.class);
        }
    }

    @Override
    public PageInfo<Publish> queryAllPublish(Integer page, Integer pagesize, String sortOrder, String state) {
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pagesize);

        PageRequest pageRequest;
        if (StrUtil.equals(sortOrder, "ascending")) {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.asc("created")));
        } else {
            pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc("created")));
        }
        List<Publish> publishList = new ArrayList<>();
        if (state.equals("all")) {
            Query query = new Query().with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);

        } else {
            Query query = Query.query(Criteria.where("state").is(state)).with(pageRequest);
            publishList = mongoTemplate.find(query, Publish.class);
        }
        pageInfo.setRecords(publishList);
        return pageInfo;


    }

    @Override
    public List<Comment> queryPublishComment(String publishId) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(publishId)));
        return mongoTemplate.find(query, Comment.class);
    }

}
